import { uploadFile } from '@/utils/requestWrapper';

import type { API } from '@/backend/typings.d';

/** 文件上传 POST /api/file/upload */
export async function uploadFileUsingPost(
  file: UniApp.UploadFileOption,
  params: API.uploadFileUsingPOSTParams,
  options?: { [key: string]: any }
) {
  return uploadFile<API.BaseResponseString_>(<string>file.filePath, {
    url: "/api/file/upload",
    formData: {
      biz: params.biz,
      ...options
    }
  });
}

/** 文件下载 GET /api/file/download */
export async function downloadFileUsingGet(
  fileUrl: string,
  options?: { [key: string]: any }
) {
  return new Promise<string>((resolve, reject) => {
    uni.downloadFile({
      url: fileUrl,
      ...options,
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.tempFilePath);
        } else {
          reject(new Error('下载失败'));
        }
      },
      fail: reject
    });
  });
} 